package cn.xspace.wnx.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;

import com.cn.wt.wtutils.AppManager;
import com.xspace.android.xafcommon.util.T;

import cn.xspace.wnx.R;

public class MainActivity extends TabActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    public static boolean isForeground = false;

    /**
     * 选项卡按钮ID数组
     **/
    private int radioButtonId[] = {R.id.tab_item_home, R.id.tab_item_order, R.id.tab_item_find,
            R.id.tab_item_me};
    /**
     * 选项卡对象
     */
    private TabHost tabhost;
    /**
     * 选项卡按钮
     */
    private RadioButton radioButton[] = new RadioButton[radioButtonId.length];
    private RadioGroup radgp_menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_main);
        initData();
        initView();
        initListener();
    }

    private void initData() {

    }

    private void initView() {
        radgp_menu = (RadioGroup) findViewById(R.id.radgp_menu);
        tabhostInit();
    }

    private void initListener() {
        radgp_menu
                .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        // TODO Auto-generated method stub
                        int currentTab = tabhost.getCurrentTab();
                        switch (checkedId) {
                            case R.id.tab_item_home:
                                setCurrentTabWithAnim(currentTab, 0, "home");
//                                tab_item_car.setBackgroundResource(R.drawable.index_car_normal);
                                break;
                            case R.id.tab_item_order:
                                setCurrentTabWithAnim(currentTab, 1, "order");
//                                tab_item_car.setBackgroundResource(R.drawable.index_car_select);
                                // radgp_menu.setVisibility(View.VISIBLE);
                                break;
                            case R.id.tab_item_find:
                                setCurrentTabWithAnim(currentTab, 2, "find");
//                                tab_item_car.setBackgroundResource(R.drawable.index_car_normal);
                                // radgp_menu.setVisibility(View.VISIBLE);
                                break;
                            case R.id.tab_item_me:
                                setCurrentTabWithAnim(currentTab, 3, "me");
//                                tab_item_car.setBackgroundResource(R.drawable.index_car_normal);
                                break;
                            default:
                                break;
                        }
                    }
                });
    }

    /***
     * 选项卡的初始化
     *
     */
    public void tabhostInit() {
        tabhost = this.getTabHost();
        tabhost.addTab(tabhost.newTabSpec("home").setIndicator("")
                .setContent(new Intent(this, HomeActivity.class)));
        tabhost.addTab(tabhost.newTabSpec("order").setIndicator("")
                .setContent(new Intent(this, AddressBookActivity.class)));
        tabhost.addTab(tabhost.newTabSpec("find").setIndicator("")
                .setContent(new Intent(this, FindActivity.class)));
        tabhost.addTab(tabhost.newTabSpec("me").setIndicator("")
                .setContent(new Intent(this, UserCenterActivity.class)));
        for (int i = 0; i < radioButton.length; i++) {
            radioButton[i] = (RadioButton) this.findViewById(radioButtonId[i]);
        }

        tabhost.setCurrentTabByTag("home");
        radioButton[0].setChecked(true);
    }

    private void setCurrentTabWithAnim(int now, int next, String tag) {
        // 这个方法是关键，用来判断动画滑动的方向
        if (now > next) {
//            tabhost.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.tab_push_right_out));
            tabhost.setCurrentTabByTag(tag);
//            tabhost.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.tab_push_right_in));
        } else {
//            tabhost.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.tab_push_left_out));
            tabhost.setCurrentTabByTag(tag);
//            tabhost.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.tab_push_left_in));
        }
    }

    private long mBackPressedTime;

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() != KeyEvent.ACTION_UP) {
            long curTime = SystemClock.uptimeMillis();
            if ((curTime - mBackPressedTime) < (3 * 1000)) {
                finish();
            } else {
                mBackPressedTime = curTime;
                T.showSingleL(getString(R.string.exit_confirm));
            }
            return true;
        }
        return super.dispatchKeyEvent(event);
    }


}
